using System;
using System.Collections.Generic;
using CMS_Admin.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using CMS_Admin.Backend.Api.ParamModel;
using Newtonsoft.Json;
using CMS_Admin.Backend.Api.Repository;
using CMS_Admin.Backend.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authentication;

namespace CMS_Admin.Backend.Controllers
{
    /// <summary>
    /// 首页获取文章的点赞数
    /// </summary>

    // [Authorize]
    [ApiController]
    [Route("articleReadeController")]
    public class ArticleReadeController : ControllerBase
    {
        private IRepository<Articles> _articleRespository;
        private IRepository<ArticleClass> _articleClass;
        private IRepository<UserArticles> _userArticles;
        private IRepository<Likes> _likes;








        private readonly IConfiguration _configuration;

        public ArticleReadeController(IConfiguration configuration, IRepository<Articles> articleRespository, IRepository<UserArticles> userArticles, IRepository<Likes> likes)
        {
            _configuration = configuration;
            _articleRespository = articleRespository;
            _likes = likes;
            _userArticles = userArticles;

        }
        /// <summary>
        /// 查找所有文章
        /// </summary>
        /// 
        /// <returns></returns>
        [HttpGet]
        public dynamic Get()
        {




            var like = from likes in _likes.Table
                       join articles in _articleRespository.Table on likes.ArticlesId equals articles.Id
                       where likes.Liked == true
                       group likes by new { articles.Id, likes.ArticlesId, articles.ArticleTitle}

                       into l
                       select new { ArticlesId=l.Key.Id, Count = l.Count(),ArticleTitle=l.Key.ArticleTitle };

            var res = new
            {
                Code = 200,
                Data = like,
                Msg = "获取文章点赞数成功^_^"
            };
            return JsonHelper.Serialize(res);
        }



    }

}